﻿
using Prism.Events;

namespace OP10.ViewModels;

public class ST13ViewModel : BindableBase, INavigationAware
{
    private readonly IRegionManager _regionManager;
    private readonly IEventAggregator _eventAggregator;

    public ST13ViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
    {
        _regionManager = regionManager;
        _eventAggregator = eventAggregator;
        _eventAggregator.GetEvent<EventWarning>().Subscribe(DoWarnings);
        //开启定时器任务
        DispatcherTimer dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Interval =  TimeSpan.FromMilliseconds(50);
        dispatcherTimer.Tick += OnTick;
        dispatcherTimer.Start();
    }
    private void DoWarnings(ObservableCollection<WarningModel> collection)
    {
        WarningData = collection;
    }
    #region 定时器
    //定时器
    private void OnTick(object? sender, EventArgs e)
    {
        Status = ST13Station_Global.stationPLCToPC.nStationStatus;

        SetStatus();

        //按钮状态
        BtnInitText = ST13Station_Global.stationPLCToPC.bInitialSTing ? "初始化中" : "初始化";
        BtnCycleStartText = ST13Station_Global.stationPLCToPC.bCycleStart ? "单循环启动中" : "单循环启动";
        BtnStationStartText = ST13Station_Global.stationPLCToPC.bSignSTstart ? "单工站启动中" : "单工站启动";

        IsBtnInit = ST13Station_Global.stationPLCToPC.bInitialSTing;
        IsBtnCycleStart = ST13Station_Global.stationPLCToPC.bCycleStart;
        IsBtnStationStart = ST13Station_Global.stationPLCToPC.bSignSTstart;

        //单工站初始化3s
        //if (isBtnInit)
        //{
        //    if (DateTime.Now - btnInitStartTime > TimeSpan.FromSeconds(3))
        //    {
        //        ST13Station_Global.stationPCToPLC.bInitialST = true;
        //        isBtnInit = false;
        //    }
        //}


        //气缸状态


        StationCode = TurntablePLCToPC_Global.StationCode;
        EncodeAngle = TurntablePLCToPC_Global.EncodeAngle;
        OrgOK = TurntablePLCToPC_Global.OrgOK;
        AutoStationCode = TurntablePLCToPC_Global.nParaData2;
        //转盘互锁条件
        Bool1 = ST01LoadJackCy.cylinderPLCToPC_Global.bOriginSensor;
        Bool2 = ST01LoadUpDownCy.cylinderPLCToPC_Global.bOriginSensor;
        Bool3 = ST03JackMaterialCy.cylinderPLCToPC_Global.bOriginSensor;
        Bool4 = ST03DownPushCy.cylinderPLCToPC_Global.bOriginSensor;
        Bool5 = ST04ElectrodeUpDownCy.cylinderPLCToPC_Global.bOriginSensor;
        Bool6 = ST04PushMaterialUpDownCy.cylinderPLCToPC_Global.bOriginSensor;
        Bool7 = ST05LightCy.cylinderPLCToPC_Global.bOriginSensor;
        Bool8 = ST06TestJackCy.cylinderPLCToPC_Global.bOriginSensor;
        Bool9 = ST06TestUpDownCy.cylinderPLCToPC_Global.bOriginSensor;
        Bool10 = ST07JackCy.cylinderPLCToPC_Global.bOriginSensor;
        Bool11 = ST07UpDownCy.cylinderPLCToPC_Global.bOriginSensor;
        Bool12 = ST09UpDownCy.cylinderPLCToPC_Global.bOriginSensor;
        Bool13 = ST10UpDownCy.cylinderPLCToPC_Global.bOriginSensor;
        Bool14 = ST11UpDownCy.cylinderPLCToPC_Global.bOriginSensor;
        Bool15 = LeftMotorPLCToPC_Global.atHome;
        Bool16 = RightMotorPLCToPC_Global.atHome;
        Bool17 = TurntablePLCToPC_Global.bIn3;
        Bool18 = TurntablePLCToPC_Global.bIn4;
        Bool19 = IO_Global.IO_R[82];
    }

    #endregion



    #region 私有方法

    /// <summary>
    /// 设置工站状态
    /// </summary>
    private void SetStatus()
    {
        if (Status == 0)
        {
            StatusText = "准备中";
        }
        else if (Status == 1)
        {
            StatusText = "故障中";

        }
        else if (Status == 2)
        {
            StatusText = "运行中";

        }

        else if (Status == 4)
        {
            StatusText = "单循环启动";

        }

        else if (Status == 5)
        {
            StatusText = "单工站启动";
        }
        else if (Status == 9)
        {
            StatusText = "未使能";
        }
    }
    #endregion
    #region 数据
    private ObservableCollection<WarningModel> _WarningData;

    public ObservableCollection<WarningModel> WarningData
    {
        get { return _WarningData ?? (_WarningData = new ObservableCollection<WarningModel>()); }
        set { SetProperty(ref _WarningData, value); }
    }

    private ObservableCollection<CylinderModel> _QGList;
    public ObservableCollection<CylinderModel> QGList
    {
        get { return _QGList ?? (_QGList = new ObservableCollection<CylinderModel>()); }
        set { SetProperty(ref _QGList, value); }
    }


    private string _StationName;
    public string StationName
    {
        get { return _StationName; }
        set { SetProperty(ref _StationName, value); }
    }

    private string _NavigateFrom;
    public string NavigateFrom
    {
        get { return _NavigateFrom; }
        set { SetProperty(ref _NavigateFrom, value); }
    }



    private int _Status;
    public int Status
    {
        get { return _Status; }
        set { SetProperty(ref _Status, value); }
    }

    private string _StatusText;
    public string StatusText
    {
        get { return _StatusText; }
        set { SetProperty(ref _StatusText, value); }
    }

    private string _BtnInitText;
    public string BtnInitText
    {
        get { return _BtnInitText; }
        set { SetProperty(ref _BtnInitText, value); }
    }

    private string _BtnCycleStartText;
    public string BtnCycleStartText
    {
        get { return _BtnCycleStartText; }
        set { SetProperty(ref _BtnCycleStartText, value); }
    }

    private string _BtnStationStartText;
    public string BtnStationStartText
    {
        get { return _BtnStationStartText; }
        set { SetProperty(ref _BtnStationStartText, value); }
    }


    private bool _IsBtnInit;
    public bool IsBtnInit
    {
        get { return _IsBtnInit; }
        set { SetProperty(ref _IsBtnInit, value); }
    }

    private bool _IsBtnCycleStart;
    public bool IsBtnCycleStart
    {
        get { return _IsBtnCycleStart; }
        set { SetProperty(ref _IsBtnCycleStart, value); }
    }


    private bool _IsBtnStationStart;
    public bool IsBtnStationStart
    {
        get { return _IsBtnStationStart; }
        set { SetProperty(ref _IsBtnStationStart, value); }
    }


    private byte _StationCode;
    public byte StationCode
    {
        get { return _StationCode; }
        set { SetProperty(ref _StationCode, value); }
    }


    private short _EncodeAngle;
    public short EncodeAngle
    {
        get { return _EncodeAngle; }
        set { SetProperty(ref _EncodeAngle, value); }
    }

    private bool _OrgOK;
    public bool OrgOK
    {
        get { return _OrgOK; }
        set { SetProperty(ref _OrgOK, value); }
    }


    private float _AutoStationCode;
    public float AutoStationCode
    {
        get { return _AutoStationCode; }
        set { SetProperty(ref _AutoStationCode, value); }
    }

    #region 转盘互锁条件
    private bool _Bool1;
    public bool Bool1
    {
        get { return _Bool1; }
        set { SetProperty(ref _Bool1, value); }
    }


    private bool _Bool2;
    public bool Bool2
    {
        get { return _Bool2; }
        set { SetProperty(ref _Bool2, value); }
    }


    private bool _Bool3;
    public bool Bool3
    {
        get { return _Bool3; }
        set { SetProperty(ref _Bool3, value); }
    }

    private bool _Bool4;
    public bool Bool4
    {
        get { return _Bool4; }
        set { SetProperty(ref _Bool4, value); }
    }


    private bool _Bool5;
    public bool Bool5
    {
        get { return _Bool5; }
        set { SetProperty(ref _Bool5, value); }
    }

    private bool _Bool6;
    public bool Bool6
    {
        get { return _Bool6; }
        set { SetProperty(ref _Bool6, value); }
    }


    private bool _Bool7;
    public bool Bool7
    {
        get { return _Bool7; }
        set { SetProperty(ref _Bool7, value); }
    }

    private bool _Bool8;
    public bool Bool8
    {
        get { return _Bool8; }
        set { SetProperty(ref _Bool8, value); }
    }

    private bool _Bool9;
    public bool Bool9
    {
        get { return _Bool9; }
        set { SetProperty(ref _Bool9, value); }
    }

    private bool _Bool10;
    public bool Bool10
    {
        get { return _Bool10; }
        set { SetProperty(ref _Bool10, value); }
    }

    private bool _Bool11;
    public bool Bool11
    {
        get { return _Bool11; }
        set { SetProperty(ref _Bool11, value); }
    }

    private bool _Bool12;
    public bool Bool12
    {
        get { return _Bool12; }
        set { SetProperty(ref _Bool12, value); }
    }

    private bool _Bool13;
    public bool Bool13
    {
        get { return _Bool13; }
        set { SetProperty(ref _Bool13, value); }
    }


    private bool _Bool14;
    public bool Bool14
    {
        get { return _Bool14; }
        set { SetProperty(ref _Bool14, value); }
    }

    private bool _Bool15;
    public bool Bool15
    {
        get { return _Bool15; }
        set { SetProperty(ref _Bool15, value); }
    }

    private bool _Bool16;
    public bool Bool16
    {
        get { return _Bool16; }
        set { SetProperty(ref _Bool16, value); }
    }

    private bool _Bool17;
    public bool Bool17
    {
        get { return _Bool17; }
        set { SetProperty(ref _Bool17, value); }
    }

    private bool _Bool18;
    public bool Bool18
    {
        get { return _Bool18; }
        set { SetProperty(ref _Bool18, value); }
    }

    private bool _Bool19;
    public bool Bool19
    {
        get { return _Bool19; }
        set { SetProperty(ref _Bool19, value); }
    }
    #endregion

    #endregion




    #region 按钮命令


    private DelegateCommand _GoBackCommand;
    public DelegateCommand GoBackCommand =>
        _GoBackCommand ?? (_GoBackCommand = new DelegateCommand(ExecuteGoBackCommand));

    void ExecuteGoBackCommand()
    {
        if (!String.IsNullOrEmpty(NavigateFrom))
        {
            _regionManager.RequestNavigate(PrismManager.MainViewRegionName, NavigateFrom);
            return;
        }

        _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.MenuView);

    }

    private DelegateCommand<string> _QGDownCommand;
    public DelegateCommand<string> QGDownCommand =>
        _QGDownCommand ?? (_QGDownCommand = new DelegateCommand<string>(ExecuteQGDownCommand));

    void ExecuteQGDownCommand(string parameter)
    {
        switch (parameter)
        {

        }
    }


    private DelegateCommand<string> _QGUpCommand;
    public DelegateCommand<string> QGUpCommand =>
        _QGUpCommand ?? (_QGUpCommand = new DelegateCommand<string>(ExecuteQGUpCommand));

    void ExecuteQGUpCommand(string parameter)
    {
        switch (parameter)
        {


        }
    }


    private DelegateCommand<string> _NavigateCommand;
    public DelegateCommand<string> NavigateCommand =>
        _NavigateCommand ?? (_NavigateCommand = new DelegateCommand<string>(ExecuteNavigateCommand));

    void ExecuteNavigateCommand(string parameter)
    {
        switch (parameter)
        {
            case "QGPingBI":
                if (!LoginInfo.IsLogin)
                {
                    MessageBox.Show("请登录用户,再进入!");
                    return;
                }
                _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST13CyManagerView);
                break;
            case "Device":
                _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST13DeviceManagerView);
                break;

        }

    }


    private DelegateCommand _GoHomeDownCmd;
    public DelegateCommand GoHomeDownCmd =>
        _GoHomeDownCmd ?? (_GoHomeDownCmd = new DelegateCommand(ExecuteGoHomeDownCmd));

    void ExecuteGoHomeDownCmd()
    {
        TurntablePCToPLC_Global.bHomeBT = true;
    }

    private DelegateCommand _GoHomeUpCmd;
    public DelegateCommand GoHomeUpCmd =>
        _GoHomeUpCmd ?? (_GoHomeUpCmd = new DelegateCommand(ExecuteGoHomeUpCmd));

    void ExecuteGoHomeUpCmd()
    {
        TurntablePCToPLC_Global.bHomeBT = false;

    }

    private DelegateCommand _JogDownCmd;
    public DelegateCommand JogDownCmd =>
        _JogDownCmd ?? (_JogDownCmd = new DelegateCommand(ExecuteJogDownCmd));

    void ExecuteJogDownCmd()
    {
        TurntablePCToPLC_Global.bJogBT = true;
    }


    private DelegateCommand _JogUpCmd;
    public DelegateCommand JogUpCmd =>
        _JogUpCmd ?? (_JogUpCmd = new DelegateCommand(ExecuteJogUpCmd));

    void ExecuteJogUpCmd()
    {
        TurntablePCToPLC_Global.bJogBT = false;

    }
    #endregion
    #region 单工站初始化/单循环启动/单工站启动


    /*
     * 初始化按钮必须按下3s才会起效果，防止误操作
     */
    private bool isBtnInit = false;
    private DateTime btnInitStartTime = DateTime.Now;

    private DelegateCommand _BtnInitMouseDownCmd;
    public DelegateCommand BtnInitMouseDownCmd =>
        _BtnInitMouseDownCmd ?? (_BtnInitMouseDownCmd = new DelegateCommand(ExecuteBtnInitMouseDownCmd));

    void ExecuteBtnInitMouseDownCmd()
    {
        //btnInitStartTime = DateTime.Now;
        //isBtnInit = true;
        ST13Station_Global.stationPCToPLC.bInitialST = true;

    }


    private DelegateCommand _BtnInitMouseUpCmd;
    public DelegateCommand BtnInitMouseUpCmd =>
        _BtnInitMouseUpCmd ?? (_BtnInitMouseUpCmd = new DelegateCommand(ExecuteBtnInitMouseUpCmd));

    void ExecuteBtnInitMouseUpCmd()
    {
        ST13Station_Global.stationPCToPLC.bInitialST = false;
        isBtnInit = false;
    }

    private DelegateCommand _BtnCycleStartMouseDownCmd;
    public DelegateCommand BtnCycleStartMouseDownCmd =>
        _BtnCycleStartMouseDownCmd ?? (_BtnCycleStartMouseDownCmd = new DelegateCommand(ExecuteBtnCycleStartMouseDownCmd));

    void ExecuteBtnCycleStartMouseDownCmd()
    {
        ST12Station_Global.stationPCToPLC.bCycleStartBT = true;
    }

    private DelegateCommand _BtnCycleStartMouseUpCmd;
    public DelegateCommand BtnCycleStartMouseUpCmd =>
        _BtnCycleStartMouseUpCmd ?? (_BtnCycleStartMouseUpCmd = new DelegateCommand(ExecuteBtnCycleStartMouseUpCmd));

    void ExecuteBtnCycleStartMouseUpCmd()
    {
        ST12Station_Global.stationPCToPLC.bCycleStartBT = false;

    }

    private DelegateCommand _BtnStationStartMouseDownCmd;
    public DelegateCommand BtnStationStartMouseDownCmd =>
        _BtnStationStartMouseDownCmd ?? (_BtnStationStartMouseDownCmd = new DelegateCommand(ExecuteBtnStationStartMouseDownCmd));

    void ExecuteBtnStationStartMouseDownCmd()
    {
        ST12Station_Global.stationPCToPLC.bSignSTstartBT = true;

    }

    private DelegateCommand _BtnStationStartMouseUpCmd;
    public DelegateCommand BtnStationStartMouseUpCmd =>
        _BtnStationStartMouseUpCmd ?? (_BtnStationStartMouseUpCmd = new DelegateCommand(ExecuteBtnStationStartMouseUpCmd));

    void ExecuteBtnStationStartMouseUpCmd()
    {
        ST12Station_Global.stationPCToPLC.bSignSTstartBT = false;

    }
    #endregion

    #region 导航

    public void OnNavigatedTo(NavigationContext navigationContext)
    {
        StationName = navigationContext.Parameters.GetValue<string>("StationName");
        NavigateFrom = navigationContext.Parameters.GetValue<string>("NavigateFrom");
    }

    public bool IsNavigationTarget(NavigationContext navigationContext)
    {
        return true;
    }

    public void OnNavigatedFrom(NavigationContext navigationContext)
    {

    }

    #endregion
}
